package com.npsngs.hawkeye;

import org.w3c.dom.Text;

import com.npsngs.hawkeye.components.BinaryzationDialog;
import com.npsngs.hawkeye.components.DialogShell;
import com.npsngs.hawkeye.components.GrayingDialog;

import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class MainActivity extends Activity {
	private GLSurfaceView mGLView;
	
	@SuppressLint("WorldWriteableFiles")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		SharedPreferences sharedPref = getSharedPreferences("history",
				Context.MODE_WORLD_WRITEABLE);
		String lastFile = sharedPref.getString("lastFile", null);
		if (null != lastFile) {
			mGLView = new MyGLSurfaceView(this);
			Bitmap bitmap = BitmapFactory.decodeFile(lastFile);
			nativeSetBitMap(bitmap);
			setContentView(mGLView);
		}
	
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.menu_openfile:
			Intent intent = new Intent();
			intent.setClass(this, FileSelectActivity.class);
			startActivityForResult(intent, 1);
			return true;
		case R.id.menu_reset_gl:
			MyGLSurfaceView.nativeGLReset();
			return true;
		case R.id.menu_graying:
			nativeGraying(0.3f, 0.6f, 0.1f);
			showDialog(DIALOG_GRAYING);
			return true;
		case R.id.menu_binaryzation:
			nativeBinaryzation(127);
			showDialog(DIALOG_BINARYZATION);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	static final int OPENFILE_REQ = 1;

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (RESULT_OK == resultCode) {
			switch (requestCode) {
			case OPENFILE_REQ:
				String path = data.getStringExtra("path");
				if (null != path) {
					mGLView = new MyGLSurfaceView(this);
					Bitmap bitmap = BitmapFactory.decodeFile(path);
					nativeSetBitMap(bitmap);
					setContentView(mGLView);
				}
				break;
			}
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	public static final int DIALOG_HISTORY = 1000;
	public static final int DIALOG_BINARYZATION = 1001;
	public static final int DIALOG_GRAYING = 1002;

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		DialogShell dialogShell = null;
		switch (id) {
		case DIALOG_HISTORY:
			break;
		case DIALOG_BINARYZATION:
			dialogShell = new BinaryzationDialog(this);
			break;
		case DIALOG_GRAYING:
			dialogShell = new GrayingDialog(this);
			break;
		default:
			break;
		}
		return dialogShell.getDialog();
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (null != mGLView) {
			mGLView.onPause();
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (null != mGLView) {
			mGLView.onResume();
		}
	}

	static {
		System.loadLibrary("hawkeye");
	}

	public static native void nativeSetBitMap(Bitmap bitmap);
	public static native void nativeGraying(float r, float g, float b);
	public static native void nativeBinaryzation(int threshold);
}
